package com.enation.app.shop.front.policyInsurance.adminController;

import com.enation.app.base.core.service.IMemberManager;
import com.enation.app.shop.core.member.model.MemberAddress;
import com.enation.app.shop.core.member.service.IMemberAddressManager;
import com.enation.app.shop.front.api.utils.ImgResponseUtil;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceTemplate;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiResponse;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiUtil;
import com.enation.app.shop.front.policyInsurance.service.IinsuranceTemplateManager;
import com.enation.eop.SystemSetting;
import com.enation.eop.sdk.context.EopSetting;
import com.enation.framework.util.DateUtil;
import com.enation.framework.util.FileUtil;
import com.enation.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/admin/insurancecom")
public class InsuranceCompanyController {
    @Autowired
    private IinsuranceTemplateManager iinsuranceTemplateManager;
    @Autowired
    private IMemberManager memberManager;
    @Autowired
    private IMemberAddressManager memberAddressManager;


    @ResponseBody
    @RequestMapping("/getTemplateByCompany")
    public List<InsuranceTemplate> getTemplateByCompany(Integer companyid){
        List<InsuranceTemplate> insuranceTemplates= iinsuranceTemplateManager.getByCompanyId(companyid);
        int i=0;
        return insuranceTemplates;
    }

    @ResponseBody
    @RequestMapping("/getAllTemplate")
    public LayuiUtil getAllTemplate(){
        List<InsuranceTemplate> insuranceTemplates= iinsuranceTemplateManager.getAll();
        int i=0;
        return LayuiResponse.getObjectMessageJson(insuranceTemplates,"sss",insuranceTemplates.size());
    }


    @ResponseBody
    @RequestMapping("/getAllTemplatetest")
    public LayuiUtil getAllTemplatetest(){
        List<MemberAddress> insuranceTemplates= memberAddressManager.getAll();
        int i=0;
        return LayuiResponse.getObjectMessageJson(insuranceTemplates,"sss",insuranceTemplates.size());
    }
    @ResponseBody
    @RequestMapping("upload/companylogo1")
    public ImgResponseUtil uploadimg1(@RequestBody MultipartFile file){
        String fileName=file.getOriginalFilename();
        if(!FileUtil.isAllowUpImg(fileName)){
            throw new IllegalArgumentException("不被允许的上传文件类型");
        }
        String ext = FileUtil.getFileExt(fileName);
        fileName = DateUtil.toString(new Date(), "mmss") + StringUtil.getRandStr(4) + "." + ext;
        String static_server_path= SystemSetting.getStatic_server_path();
        String filePath =  static_server_path + "/attachment/";
        String subFolder="companylogo";
        if(subFolder!=null){
            filePath+=subFolder +"/";
        }
        String timePath=this.getTimePath();
        String path  = EopSetting.FILE_STORE_PREFIX+ "/attachment/" +(subFolder==null?"":subFolder)+"/"+timePath+"/"+fileName;
        filePath +=timePath;
        filePath += fileName;
        try {
            FileUtil.write(file.getInputStream(),filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String companylogo=path;
        ImgResponseUtil imgResponseUtil=new ImgResponseUtil();
        imgResponseUtil.setSuccess(true);
        imgResponseUtil.setResult(companylogo);
        return imgResponseUtil;
    }
    private String getTimePath(){
        Calendar now = Calendar.getInstance();
        int year=now.get(Calendar.YEAR);
        int month=now.get(Calendar.MONTH) + 1;
        int date=now.get(Calendar.DAY_OF_MONTH);
        int minute=now.get(Calendar.HOUR_OF_DAY);
        String filePath="";
        if(year!=0){
            filePath+=year+"/";;
        }
        if(month!=0){
            filePath+=month+"/";;
        }
        if(date!=0){
            filePath+=date+"/";;
        }
        if(minute!=0){
            filePath+=minute+"/";;
        }
        return  filePath;
    }
}
